#include "stdafx.h"
#include "ui_window.h"
#include "ui_window_manager.h"

_SSL_BEGIN
_SSUIL_BEGIN

#define DEFAULT_SS_UI_CLASS_NAME _T("__SSUIDefaultClassName__")
#define SS_UI_OBJECT_PTR_PROPNAME _T("__SSUIObjectPtrProp__")

RawWindow::RawWindow(const tchar *class_name /* = NULL */)
{
	inst_ = NULL;
	window_h_ = NULL;
	transparent_color_ = 0;
	SetWindowClassName(class_name);
}

RawWindow::~RawWindow()
{
	Destroy();
	inst_ = NULL;
}


void RawWindow::SetWindowClassName(const tchar *class_name)
{
	if(class_name == NULL || _tcslen(class_name) == 0)
		class_name = DEFAULT_SS_UI_CLASS_NAME;
	_tcscpy_s(class_name_, class_name);
}

void RawWindow::SetInstance(HINSTANCE inst)
{
	inst_ = inst;
}

void RawWindow::SetTransparentColor(unsigned long transparent_color)
{
	transparent_color_ = transparent_color;
}

bool RawWindow::Destroy()
{
	if(window_h_ == NULL)
		return true;
	if(::IsWindow(window_h_))
		::DestroyWindow(window_h_);
	window_h_ = NULL;
	return true;
}

HWND RawWindow::GetWindow(HWND parent_h /* = NULL */, UINT id /* = 0 */)
{
	if(::IsWindow(window_h_))
	{
		if(parent_h == NULL || parent_h == ::GetParent(window_h_))
		{
			if(parent_h != NULL && id != 0)
			{
				::SetWindowLong(window_h_, GWL_ID, id);
			}
			return window_h_;
		}
		Destroy();
	}

	window_h_ = NULL;

	tstring class_name_with_transparent_color = GetClassNameWithTransparentColor();
	if(!RegisterClassIfNecessary(GetInstance(), class_name_with_transparent_color.c_str(), transparent_color_))
		return NULL;

	if(parent_h == NULL || id == 0)
	{
		window_h_ = ::CreateWindowEx(0, class_name_with_transparent_color.c_str(), _T(""),
			WS_POPUP|WS_CLIPCHILDREN|WS_CLIPSIBLINGS, 0, 0, 0, 0,
			parent_h, NULL, GetInstance(), NULL);
	}else
	{
		window_h_ = ::CreateWindowEx(0, class_name_with_transparent_color.c_str(), _T(""),
			WS_CHILD|WS_CLIPCHILDREN|WS_CLIPSIBLINGS, 0, 0, 0, 0,
			parent_h, (HMENU)id, GetInstance(), NULL);
	}

	return window_h_;
}

bool RawWindow::IsValid() const
{
	return ::IsWindow(window_h_) ? true : false;
}

void RawWindow::BringToFront()
{
	if(!IsValid())
		return;
	::SetForegroundWindow(window_h_);
	::SetWindowPos(window_h_, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE|SWP_SHOWWINDOW);
}

HINSTANCE RawWindow::GetInstance()
{
	if(inst_ != NULL)
		return inst_;
	return ::GetModuleHandleA(NULL);
}

tstring RawWindow::GetClassNameWithTransparentColor()
{
	tstring ret = class_name_;
	ret += _T("-");
	ret += int_to_tstring(transparent_color_);
	return ret;
}

bool RawWindow::RegisterClassIfNecessary(HINSTANCE inst, const tchar *class_name, unsigned long transparent_color)
{
	WNDCLASSEX wnd_class_ex = { sizeof(WNDCLASSEX) };
	if(::GetClassInfoEx(inst, class_name, &wnd_class_ex))
		return true;
	memset(&wnd_class_ex, 0, sizeof(WNDCLASSEX));
	wnd_class_ex.cbSize = sizeof(WNDCLASSEX);
	wnd_class_ex.style = CS_HREDRAW|CS_VREDRAW;
	wnd_class_ex.lpfnWndProc = ___WindowProc___;
	wnd_class_ex.hInstance = inst;
	wnd_class_ex.hCursor = ::LoadCursor(NULL, IDC_ARROW);
	wnd_class_ex.hbrBackground = (HBRUSH)CreateSolidBrush(DWORD2RGB(transparent_color));
	wnd_class_ex.lpszClassName = class_name;
	return ::RegisterClassEx(&wnd_class_ex) != 0;
}

LRESULT CALLBACK RawWindow::___WindowProc___(HWND window_h, UINT msg, WPARAM wparam, LPARAM lparam)
{
	return ::DefWindowProcA(window_h, msg, wparam, lparam);
}


UIWindow::UIWindow()
{
	wnd_proc_ = NULL;
	is_main_window_ = false;
	win_man_.assign_no_ref(new UIWindowManager);
	release_on_destory_message_ = false;
}

UIWindow::~UIWindow()
{
	if(win_man_ != NULL)
	{
		win_man_->SetContent(NULL);
	}
	Uninit();
	win_man_ = NULL;
}


bool UIWindow::SetWindowPresetLeftTopPoint(const UIPoint &pt)
{
	win_pt_ = pt;
	return true;
}

bool UIWindow::SetWindowPresetSize(const UISize &st)
{
	win_sz_ = st;
	return true;
}

bool UIWindow::ShowWindow(bool show)
{
	return ShowWindowEx(show, false, false, false);
}

bool UIWindow::ShowWindowEx(bool show, bool center, bool init, bool show_noactive)
{
	if(!raw_window_.IsValid())
		return false;

	if(win_man_ != NULL)
	{
		scoped_ref_ptr<IUIControlInterface> content = win_man_->GetContent();
		if(content != NULL)
			content->SetNeedUpdate(true);
	}

	if(!show)
	{
		::ShowWindow(raw_window_.GetWindow(), SW_HIDE);
		return true;
	}

	UINT swp_flags = SWP_SHOWWINDOW;
	if(show_noactive)
		swp_flags |= SWP_NOACTIVATE;

	UISize win_sz = win_sz_;
	if(center || init)
	{
		UIRect rt;
		::GetWindowRect(raw_window_.GetWindow(), &rt);
		if(rt.Width() <= 0 || rt.Height() <= 0)
		{
			if(win_sz.cx <= 0)
				win_sz.cx = 10;
			if(win_sz.cy <= 0)
				win_sz.cy = 10;
		}
	}

	if(!center && !init)
	{
		::ShowWindow(raw_window_.GetWindow(), (show_noactive ? SW_SHOWNOACTIVATE : SW_SHOW));
		return true;
	}

	if(center)
	{
		UIRect rt_win;
		win_center_window(::GetParent(raw_window_.GetWindow()), win_sz, rt_win);
		::SetWindowPos(raw_window_.GetWindow(), NULL, rt_win.left, rt_win.top,
			rt_win.Width(), rt_win.Height(), SWP_NOZORDER|swp_flags);
		return true;
	}

	if(win_sz.cx == 0 || win_sz.cy == 0)
	{
		if(win_sz.cx <= 0)
			win_sz.cx = 10;
		if(win_sz.cy <= 0)
			win_sz.cy = 10;
	}
	::SetWindowPos(raw_window_.GetWindow(), NULL, win_pt_.x, win_pt_.y,
		win_sz.cx, win_sz.cy, SWP_NOZORDER|swp_flags);

	if(win_man_->IsTranslucent())
	{
		::InvalidateRect(raw_window_.GetWindow(), NULL, FALSE);
	}

	return true;
}

bool UIWindow::IsWindowVisibled()
{
	if(!raw_window_.IsValid())
		return false;
	return BOOL2bool(::IsWindowVisible(raw_window_.GetWindow()));
}

bool UIWindow::IsValid() const
{
	return raw_window_.IsValid();
}

bool UIWindow::SetReleaseOnDestoryMessage(bool release_ref)
{
	if(release_ref)
	{
		if(release_on_destory_message_)
			return true;
		if(!IsValid())
			return false;
		this->add_ref();
		release_on_destory_message_ = true;
		return true;
	}

	if(release_on_destory_message_)
	{
		this->release();
		release_on_destory_message_ = false;
	}
	return true;
}

bool UIWindow::IsReleaseOnDestoryMessage() const
{
	return release_on_destory_message_;
}

bool UIWindow::Destroy()
{
	if(!IsValid())
		return true;
	return ::DestroyWindow(raw_window_.GetWindow()) ? true : false;;
}

bool UIWindow::Init(HWND parent_win, UINT id, HINSTANCE inst, unsigned long transparent_color,
	IUIWindowBgCanvasInterface *bg_canvas,
	IUIResourceRenderInterface *res_ren, IUIDiskFilesManagerInterface *disk_man,
	IUIBuilderInterface *builder)
{
	if(bg_canvas == NULL || res_ren == NULL || disk_man == NULL)
	{
		return false;
	}

	if(parent_win != NULL)
	{
		if(!::IsWindow(parent_win))
		{
			return false;
		}
	}

	Uninit();

	bool ret = false;
	raw_window_.SetInstance(inst);
	raw_window_.SetTransparentColor(transparent_color);
	HWND window_h = raw_window_.GetWindow(parent_win, id);
	if(!::IsWindow(window_h))
	{
		ret = false;
		goto finish;
	}

	wnd_proc_ = (WNDPROC)::SetWindowLongPtr(window_h, GWLP_WNDPROC, (LONG)___HookedWindowProc___);
	if(wnd_proc_ == NULL)
	{
		ret = false;
		goto finish;
	}

	if(!::SetProp(window_h, SS_UI_OBJECT_PTR_PROPNAME, (HANDLE)this))
	{
		ret = false;
		goto finish;
	}

	ret = win_man_->Init(window_h, inst, bg_canvas, res_ren, disk_man, builder);

	ret = true;
finish:
	if(!ret)
	{
		Uninit();
	}

	return ret;
}

void UIWindow::Uninit()
{
	win_man_->Uninit();

	if(raw_window_.IsValid())
	{
		HWND window_h = raw_window_.GetWindow();
		if(window_h != NULL)
		{
			::SetProp(window_h, SS_UI_OBJECT_PTR_PROPNAME, NULL);
			::SetWindowLongPtr(window_h, GWLP_WNDPROC, (LONG)NULL);
		}
	}

	wnd_proc_ = NULL;

	raw_window_.Destroy();
}

bool UIWindow::ApplyAttributes(IUIBuilderInterface *ui_builder, 
	IUIXMLDocumentInterface *xml_document, UIXmlNode node)
{
	if(ui_builder == NULL || xml_document == NULL)
		return false;
	if(xml_document->NodeName(node) == NULL)
		return false;

	UIXmlAttribute attribute = NULL;
	attribute = xml_document->FirstAttribute(node, "type");
	if(attribute == NULL)
		return false;

	attribute = xml_document->FirstAttribute(node, "name");
	if(attribute != NULL)
	{
		SetName(auto_cvt(xml_document->AttributeValue(attribute)).c_tstr());
	}

	attribute = xml_document->FirstAttribute(node, "left_top");
	if(attribute != NULL)
	{
		int l = 0, t = 0;
		if(UIValueParseUtil::ParseIntInt(xml_document->AttributeValue(attribute), l, t) == 2)
		{
			UIPoint pt;
			pt.x = l;
			pt.y = t;
			SetWindowPresetLeftTopPoint(pt);
		}
	}

	attribute = xml_document->FirstAttribute(node, "size");
	if(attribute != NULL)
	{
		UISize st;
		if(UIValueParseUtil::ParseSize(xml_document->AttributeValue(attribute), st) == 1)
		{
			SetWindowPresetSize(st);
		}
	}

	attribute = xml_document->FirstAttribute(node, "main_window");
	if(attribute != NULL)
	{
		bool b = false;
		if(UIValueParseUtil::ParseBool(xml_document->AttributeValue(attribute), b) == 1)
			SetMainWindow(b);
	}

	//attribute = xml_document->FirstAttribute(node, "auto_width_size");
	//if(attribute != NULL)
	//{
	//	int min_v = 0, max_v = 0;
	//	if(UIValueParseUtil::ParseIntInt(xml_document->AttributeValue(attribute), min_v, max_v) == 2)
	//	{
	//		win_man_->set_window_auto_size_width(min_v, max_v);
	//	}
	//}

	//attribute = xml_document->FirstAttribute(node, "auto_height_size");
	//if(attribute != NULL)
	//{
	//	int min_v = 0, max_v = 0;
	//	if(UIValueParseUtil::ParseIntInt(xml_document->AttributeValue(attribute), min_v, max_v) == 2)
	//	{
	//		win_man_->set_window_auto_size_height(min_v, max_v);
	//	}
	//}

	//attribute = xml_document->FirstAttribute(node, "tooptip_window");
	//if(attribute != NULL)
	//{
	//	auto_cvt tooptip_window_str(xml_document->AttributeValue(attribute));
	//	tstrings vs = str_split(tooptip_window_str.c_tstr(), _T(':'));
	//	if(vs.size() != 2)
	//	{
	//		TraceVW(_T("parse tooptip_window failed for %s"), tooptip_window_str.c_tstr());
	//	}else
	//	{
	//		ui_unknown_interface_auto_ptr tooltip_win_unk;
	//		if(!ui_builder->build_window(xml_document, vs[0].c_str(),
	//			true, win_man_->get_render_manager(), tooltip_win_unk, win_man_->get_window(), 0,
	//			win_man_->get_instance()) || tooltip_win_unk == NULL)
	//		{
	//			TraceVE(_T("build tooltip failed for %s"), vs[0].c_str());
	//		}else
	//		{
	//			tooltip_ = dynamic_cast<ui_window *>((ui_unknown_interface *)tooltip_win_unk);
	//			if(tooltip_ == NULL || tooltip_->GetWindowManager() == NULL)
	//			{
	//				TraceVW(_T("not a tooptip window failed for %s"), vs[0].c_str());
	//			}else
	//			{
	//				HWND wnd_h = tooltip_->get_raw_window()->get_window();
	//				::SetWindowLong(wnd_h, GWL_EXSTYLE, ::GetWindowLong(wnd_h, GWL_EXSTYLE)|WS_EX_NOACTIVATE);
	//				win_man_->set_tooltip_window(tooltip_, vs[1].c_str());
	//			}
	//		}
	//	}
	//}

	attribute = xml_document->FirstAttribute(node, "background_image");
	if(attribute != NULL)
	{
		UIPresetImage i;
		if(UIValueParseUtil::ParseImage(xml_document->AttributeValue(attribute), i) == 1)
		{
			bool translucent = false;
			attribute = xml_document->FirstAttribute(node, "translucent");
			if(attribute != NULL)
			{
				bool b = false;
				if(UIValueParseUtil::ParseBool(xml_document->AttributeValue(attribute), b) == 1)
					translucent = b;
			}

			win_man_->SetBackgroundImage(i, translucent);

			attribute = xml_document->FirstAttribute(node, "noalpha_control_name");
			if(attribute != NULL)
			{
				tstring nm = auto_cvt(xml_document->AttributeValue(attribute)).c_tstr();
				str_trim(nm, NULL);
				if(!nm.empty())
				{
					win_man_->SetNoalphaControlOnTranslucentMode(nm);
				}
			}
		}
	}

	attribute = xml_document->FirstAttribute(node, "transparent_color");
	if(attribute != NULL)
	{
		if(_stricmp(xml_document->AttributeValue(attribute), "none") == 0)
		{
			win_man_->SetTransparentColor(INVALID_DWORD_COLOR_VALUE);
		}else
		{
			unsigned long c = 0;
			if(UIValueParseUtil::ParseColor(xml_document->AttributeValue(attribute), c) == 1)
				win_man_->SetTransparentColor(c);
		}
	}

	attribute = xml_document->FirstAttribute(node, "maxmize_box");
	if(attribute != NULL)
	{
		bool b = false;
		if(UIValueParseUtil::ParseBool(xml_document->AttributeValue(attribute), b) == 1)
		{
			SS_ASSERT(raw_window_.IsValid());
			HWND window = raw_window_.GetWindow();
			LONG style = ::GetWindowLong(window, GWL_STYLE);
			if(b)
			{
				style |= WS_MAXIMIZEBOX;
				win_man_->SetFullScreen(false);
			}else
			{
				style &= ~WS_MAXIMIZEBOX;
			}
			::SetWindowLong(window, GWL_STYLE, style);
		}
	}

	attribute = xml_document->FirstAttribute(node, "minimize_box");
	if(attribute != NULL)
	{
		bool b = false;
		if(UIValueParseUtil::ParseBool(xml_document->AttributeValue(attribute), b) == 1)
		{
			SS_ASSERT(raw_window_.IsValid());
			HWND window = raw_window_.GetWindow();
			LONG style = ::GetWindowLong(window, GWL_STYLE);
			if(b)
				style |= WS_MINIMIZEBOX;
			else
				style &= ~WS_MINIMIZEBOX;
			::SetWindowLong(window, GWL_STYLE, style);
		}
	}

	attribute = xml_document->FirstAttribute(node, "full_screen");
	if(attribute != NULL)
	{
		bool b = false;
		if(UIValueParseUtil::ParseBool(xml_document->AttributeValue(attribute), b) == 1)
		{
			win_man_->SetFullScreen(b);
		}
	}

	return true;
}

void UIWindow::SetName(const tchar *name)
{
	if(name == NULL)
		name = _T("");
	name_ = name;
}

const tchar* UIWindow::GetName() const
{
	return name_.c_str();
}

void UIWindow::SetMainWindow(bool is_main)
{
	is_main_window_ = is_main;
}

bool UIWindow::IsMainWindow() const
{
	return is_main_window_;
}

RawWindow* UIWindow::GetRawWindow()
{
	return &raw_window_;
}

void UIWindow::SetTooltip(UIWindow *tooltip)
{
	tooltip_ = tooltip;
}

scoped_ref_ptr<IUIManagerInterface> UIWindow::GetWindowManager()
{
	return win_man_;
}


LRESULT UIWindow::WindowProc(UINT msg, WPARAM wparam, LPARAM lparam, bool &handled)
{
	return OnWindowProc(msg, wparam, lparam, handled);
}

LRESULT UIWindow::OnWindowProc(UINT msg, WPARAM wparam, LPARAM lparam, bool &handled)
{
	LRESULT ret = 0;

	switch(msg)
	{
	case WM_DESTROY:
		if(is_main_window_)
		{
			PostQuitMessage(0);
		}
		if(release_on_destory_message_)
		{
			this->release();
			release_on_destory_message_ = false;
		}
		break;
	}

	if(handled)
		return ret;

	LRESULT result = 0;
	handled = win_man_->HandleMessage(msg, wparam, lparam, result);
	return result;
}

LRESULT CALLBACK UIWindow::___HookedWindowProc___(HWND window_h, UINT msg, WPARAM wparam, LPARAM lparam)
{
	LRESULT ret = 0;

	scoped_ref_ptr<UIWindow> p_this = (UIWindow*)::GetProp(window_h, SS_UI_OBJECT_PTR_PROPNAME);
	bool handled = false;
	if(p_this != NULL)
		ret = p_this->WindowProc(msg, wparam, lparam, handled);
	switch(msg)
	{
	case WM_NCPAINT:
		// return 0 is XP
		break;
	case WM_DESTROY:
		::SetProp(window_h, SS_UI_OBJECT_PTR_PROPNAME, NULL);
	}

	if(handled)
		return ret;
	ret = ::DefWindowProc(window_h, msg, wparam, lparam);

	return ret;
}

//
//
//
UIDialog::UIDialog(const tstring &prefix_end_dlg_button_name /*= _T("")*/)
	: prefix_end_dlg_button_name_(prefix_end_dlg_button_name)
{
	window_h_ = NULL;
	win_man_->AppendUINotifyReceiver(this);
}

UIDialog::~UIDialog()
{

}

bool UIDialog::Init(HWND parent_win, UINT id, HINSTANCE inst, unsigned long transparent_color,
	IUIWindowBgCanvasInterface *bg_canvas,
	IUIResourceRenderInterface *res_ren, IUIDiskFilesManagerInterface *disk_man,
	IUIBuilderInterface *builder)
{
	if(bg_canvas == NULL || res_ren == NULL || disk_man == NULL)
	{
		return false;
	}

	if(parent_win != NULL)
	{
		if(!::IsWindow(parent_win))
		{
			return false;
		}
	}

	Uninit();

	parent_window_h_ = parent_win;
	inst_ = inst;
	bg_canvas_ = bg_canvas;
	render_manager_ = res_ren;
	disk_files_manager_ = disk_man;
	builder_ = builder_;
	return true;
}

void UIDialog::Uninit()
{

}

RawWindow* UIDialog::GetRawWindow()
{
	return NULL;
}

void UIDialog::SetWindowSize(UISize &sz)
{
	win_sz_ = sz;
	if(!::IsWindow(window_h_))
		return;

	UIRect rt_win;
	win_center_window(parent_window_h_, win_sz_, rt_win);

	::SetWindowPos(window_h_, NULL, rt_win.left, rt_win.top, rt_win.Width(), rt_win.Height(), SWP_NOZORDER);
}

bool UIDialog::ShowWindow(int show_method)
{
	bool ret = false;
	HGLOBAL global_h = NULL;
	LPDLGTEMPLATE dt = NULL;
	LPWORD lpw = NULL;
	LPWSTR lpwsz = NULL;

	global_h = GlobalAlloc(GMEM_ZEROINIT, 1024);
	if(global_h == NULL)
		goto finish;

	dt = (LPDLGTEMPLATE)GlobalLock(global_h);

	dt->style = WS_POPUP;
	dt->dwExtendedStyle = 0;
	dt->cdit = 0; // number of controls
	dt->x = 10;
	dt->y = 10;
	dt->cx = 10;
	dt->cy = 10;

	lpw = (LPWORD)(dt + 1);
	*lpw++ = 0; // no menu
	*lpw++ = 0; // predefined dialog box class (by default)

	lpwsz = (LPWSTR)lpw;
	*lpwsz = L'\0';

	lpw += 2;

	GlobalUnlock(global_h);
	dt = NULL;

	ret = DialogBoxIndirectParam(inst_, (LPDLGTEMPLATE)global_h, parent_window_h_, (DLGPROC)___HookedDialogProc___,
		(LPARAM)this) > 0;

finish:
	if(global_h != NULL)
	{
		GlobalFree(global_h);
		global_h = NULL;
	}

	return ret;
}

void UIDialog::OnNotify(UINotify &notify)
{
	if(notify.sender == NULL)
		return;
	if(notify.type != _T("click"))
		return;
	tstring name = notify.sender->GetName();
	if(name.empty())
		return;
	if(!prefix_end_dlg_button_name_.empty())
	{
		if(name.find(prefix_end_dlg_button_name_) != 0)
			return;
	}
	result_ = name;
	::EndDialog(window_h_, IDOK);
}

tstring UIDialog::DoModal()
{
	if(!ShowWindow(SW_SHOW))
		return _T("");
	return GetResult();
}

tstring UIDialog::GetResult()
{
	return result_;
}

bool UIDialog::ReallyInit(HWND window_h)
{
	if(!::IsWindow(window_h))
	{
		return false;
	}

	if(render_manager_ == NULL || disk_files_manager_ == NULL)
	{
		return false;
	}

	if(!::SetProp(window_h, SS_UI_OBJECT_PTR_PROPNAME, (HANDLE)this))
	{
		return false;
	}

	window_h_ = window_h;
	SetWindowSize(win_sz_);

	return win_man_->Init(window_h, inst_, bg_canvas_, render_manager_,
		disk_files_manager_, builder_);
}

INT_PTR CALLBACK UIDialog::___HookedDialogProc___(HWND window_h, UINT msg, WPARAM wparam, LPARAM lparam)
{
	switch(msg)
	{
	case WM_INITDIALOG:
		{
			UIDialog *dlg = (UIDialog*)lparam;
			SS_ASSERT(dlg != NULL);
			dlg->ReallyInit(window_h);
		}
		break;
	}

	LRESULT ret = 0;
	scoped_ref_ptr<UIDialog> p_this = (UIDialog*)::GetProp(window_h, SS_UI_OBJECT_PTR_PROPNAME);
	if(p_this == NULL)
		return FALSE;

	bool handled = false;
	if(p_this != NULL)
		ret = p_this->WindowProc(msg, wparam, lparam, handled);
	switch(msg)
	{
	case WM_NCPAINT:
		// return 0 is XP
		break;
	case WM_DESTROY:
		::SetProp(window_h, SS_UI_OBJECT_PTR_PROPNAME, NULL);
		break;
	}
	if(handled)
	{
		SetWindowLong(window_h, DWL_MSGRESULT, ret);
		return TRUE;
	}
	return FALSE;
}

//
//
//
bool ui_menu_window::Init(HWND parent_win, UINT id, HINSTANCE inst, unsigned long transparent_color,
	IUIWindowBgCanvasInterface *bg_canvas,
	IUIResourceRenderInterface *res_ren, IUIDiskFilesManagerInterface *disk_man,
	IUIBuilderInterface *builder)
{
	if(!__super::Init(parent_win, id, inst, transparent_color, bg_canvas, res_ren, disk_man, builder))
		return false;
	SS_ASSERT(GetWindowManager() != NULL);
	HWND window = GetWindowManager()->GetWindow();
	if(window != NULL)
	{
		::SetWindowLong(window, GWL_EXSTYLE, ::GetWindowLong(window, GWL_EXSTYLE)|WS_EX_TOOLWINDOW);
	}
	return true;
}

LRESULT ui_menu_window::OnWindowProc(UINT msg, WPARAM wparam, LPARAM lparam, bool &handled)
{
	SS_ASSERT(GetWindowManager() != NULL);
	HWND window = GetWindowManager()->GetWindow();

	switch(msg)
	{
	case WM_ACTIVATE:
		if(LOWORD(wparam) == WA_INACTIVE)
		{
			::ShowWindow(window, SW_HIDE);
			UINotify nty;
			nty.type = _T("window_hide");
			nty.lparam = (__int64)(UIWindow*)this;
			GetWindowManager()->SendNotify(nty);
		}
		break;
	}
	return __super::OnWindowProc(msg, wparam, lparam, handled);
}

_SSUIL_END
_SSL_END
